<template>
    <div>
        <div>
            <el-config-provider :locale="zhCn"> <!--《:locale="zhCn 》设置语言-->

                <el-scrollbar height="70vh">
                    <!-- 加班申请 -->
                    <el-row :gutter="24">
                        <el-col :span="24">
                            <div class="grid-content ep-bg-purple-light" style="padding: 15px 0;">
                                <el-form-item label="加班申请" label-position="top"
                                    :style="{ fontSize: '15px', fontWeight: 'bold', borderBottom: '1px dashed #e4e7ed' }">
                                    <div class="progress-bar">
                                        <div class="progress"></div>
                                    </div>
                                </el-form-item>

                            </div>
                        </el-col>

                        <!--  -->
                        <el-col :span="12">
                            <div class="grid-content ep-bg-purple-light"><el-form-item label="加班单号"
                                    label-position="top">
                                    <el-input placeholder="" v-model="overtimeApplicationDetails.overtimeId"
                                        style="width: 354px;" disabled />
                                </el-form-item>
                            </div>
                        </el-col>
                        <el-col :span="12">
                            <div class="grid-content ep-bg-purple-light"><el-form-item label="姓名" label-position="top">
                                    <el-input placeholder="" v-model="user.employeeName" style="/*width: 354px;*/"
                                        disabled />
                                </el-form-item>
                            </div>
                        </el-col>

                        <el-col :span="12">
                            <div class="grid-content ep-bg-purple-light"><el-form-item label="所在部门"
                                    label-position="top">
                                    <el-input placeholder="所在部门" v-model="user.department.deptName"
                                        style="/*width: 260px;*/" disabled />
                                </el-form-item>
                            </div>
                        </el-col>
                        <el-col :span="12">
                            <div class="grid-content ep-bg-purple-light"><el-form-item label="申请日期"
                                    label-position="top">
                                    <el-input placeholder="" v-model="overtimeApplicationDetails.applicationDate"
                                        style="/*width: 260px;*/" disabled />
                                </el-form-item>
                            </div>
                        </el-col>

                        <el-col :span="12">
                            <div class="grid-content ep-bg-purple-light"><el-form-item label="加班类型"
                                    label-position="top">
                                    <el-tag :type="getOvertimeTypeTag(overtimeApplicationDetails.overtimeType)"
                                        effect="dark">
                                        {{ overtimeApplicationDetails.overtimeType }}
                                    </el-tag>
                                </el-form-item>
                            </div>
                        </el-col>
                        <el-col :span="12">
                            <div class="grid-content ep-bg-purple-light"><el-form-item label="归属年份"
                                    label-position="top">
                                    <el-input placeholder="" v-model="overtimeApplicationDetails.attendanceYear"
                                        style="/*width: 260px;*/" disabled />
                                </el-form-item>
                            </div>
                        </el-col>

                        <el-col :span="24">
                            <div class="grid-content ep-bg-purple-light">
                                <el-form-item label="加班明细" label-position="top">
                                    <el-table :data="overtimeData.records" border stripe>
                                        <el-table-column type="index" width="50" />
                                        <el-table-column v-for="col in overtime_columns" :prop="col.id" :key="col.id"
                                            :label="col.label" :width="col.width">
                                        </el-table-column>
                                    </el-table>
                                </el-form-item>
                            </div>
                            <div>
                                <el-pagination @current-change="loadOvertimeDetails"
                                    v-model:currentPage="overtimeData.current" :page-sizes="[2, 4, 6, 8, 10]"
                                    :page-size="overtimeData.size" :total="overtimeData.total"
                                    layout="total, prev, pager, next">
                                </el-pagination>
                            </div>
                        </el-col>

                        <!--  -->
                        <el-col :span="24">
                            <!-- 空白分隔区域 -->
                            <div style="height: 10px; border-top: 0px dashed #e4e7ed; margin: 6px 0;"></div>
                        </el-col>

                        <el-col :span="12">
                            <div class="grid-content ep-bg-purple-light"><el-form-item label="加班总天数"
                                    label-position="top">
                                    <el-input placeholder="暂无内容" v-model="overtimeApplicationDetails.totalDay"
                                        style="/*width: 260px;*/" disabled />
                                </el-form-item>
                            </div>
                        </el-col>
                        <el-col :span="12">
                            <div class="grid-content ep-bg-purple-light"><el-form-item label="加班事由"
                                    label-position="top">
                                    <el-input v-model="overtimeApplicationDetails.reason" style="width: 633px;"
                                        :rows="6" placeholder="" disabled />
                                </el-form-item>
                            </div>
                        </el-col>

                    </el-row>
                </el-scrollbar>

            </el-config-provider>
        </div>
    </div>
</template>

<script setup>
import { ref, defineProps } from 'vue'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { ElMessage, ElMessageBox } from 'element-plus'
import router from '@/router';
import request from '@/api/request';

// 获取session中的用户信息 - 需要解析JSON字符串
let user = JSON.parse(sessionStorage.getItem('user') || '{}');

const props = defineProps({
    overtimeApplicationDetails: {
        type: Object,
        default: () => ({})
    }
})

const overtime_columns = [
    { id: "startTime", label: "加班开始时间" },
    { id: "endTime", label: "加班结束时间" },
    { id: "week", label: "星期" },
    { id: "duration", label: "加班时长（h）" },
    { id: "overtimeDate", label: "加班天数" },
    { id: "attendanceCycle", label: "归属考勤周期" },
]

const overtimeData = ref({
    current: 1,
    size: 3,
    total: 0,
    records: []
})

let loadOvertimeDetails = () => {
    request({
        url: '/overtimeApplication/overtimeDetails',
        method: 'get',
        params: {
            current: overtimeData.value.current,
            size: overtimeData.value.size,
            applicationDate: props.overtimeApplicationDetails.applicationDate
        }
    }).then(resp => {
        overtimeData.value = resp.data
        calculateOvertimeDays();
    })
}

// 计算加班天数
const calculateOvertimeDays = () => {
    if (!overtimeData.value.records || overtimeData.value.records.length === 0) {
        return;
    }
    
    overtimeData.value.records.forEach(record => {
        const duration = parseFloat(record.duration);
        let overtimeDays = 0;
        
        if (duration >= 8) {
            overtimeDays = 1;
        } else if (duration >= 4) {
            overtimeDays = 0.5;
        } else {
            overtimeDays = 0;
        }
        
        record.overtimeDate = overtimeDays.toFixed(1);
    });
}

const isVisible = ref(false)
// 复制添加
const add = () => {
    isVisible.value = true
    router.push('./insert.vue')
}
//修改显示
const EditDisplay = ref(false)
//编辑
const Editor = () => {
    EditDisplay.value = true
}

//删除操作
const open = () => {
    ElMessageBox.confirm(
        '你确定要删除吗?',
        '通知',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error',
        }
    )
        .then(() => {
            ElMessage({
                type: 'success',
                message: '删除成功',

            })
            emit("ref-table")
        })
        .catch(() => {
            ElMessage({
                type: 'warning',
                message: '取消操作',
            })
        })
}

const getOvertimeTypeTag = (overtimeType) => {
    // 你可以根据实际的加班类型值来定义不同类型
    switch (overtimeType) {
        case "工作日":
            return "primary";
        case "周末":
            return "success";
        case "法定节假日":
            return "danger";
        default:
            return "info";
    }
};

loadOvertimeDetails();
</script>

<style scoped>
/* 外层容器，可控制整体条的高度、圆角等 */
.progress-bar {
    height: 6px;
    background-color: #ccebf7;
    border-radius: 3px;
    display: flex;
    width: 100%;
    /* 确保进度条容器宽度占满父容器 */
    overflow: hidden;
    /* 防止子元素溢出 */
}

.progress {
    width: 8%;
    height: 10;
    background-color: #61c3ea;
    border-radius: 3px 0 0 3px;
    transition: width 0.3s ease;
    /* 添加宽度变化的过渡效果 */
}

.bottom-submit-bar {
    /* position: fixed; */
    /* 固定定位 */
    bottom: 0;
    /* 靠底部 */
    left: 0;
    width: 100%;
    background-color: #fff;
    /* 背景色，可根据需求调整 */
    border-top: 1px solid #eaeaea;
    /* 顶部边框，与页面区分 */
    padding: 12px 24px;
    /* 内边距，适配截图里的间距风格 */
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    /* 按钮靠左，可根据实际需求调整 */
    z-index: 10;
    /* 提高层级，避免被其他内容覆盖 */
}

.el-table {
    width: 100% !important;
}

.el-table th,
.el-table td {
    padding: 12px 8px;
}
</style>
